﻿// ———————————————————————–
// <copyright file="TEP.cs" company="The MITRE Corporation">
//    Copyright (c) 2012 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// TEP.cs - Library Class for the TEP Message
// Project: IC.NET - TEP Library
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010 Beta 2
// Author:      Lizzie DeYoung MITRE
//              Brian Wilkins  MITRE

/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2012 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-12-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)using System;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Reflection;

namespace EDXLSharp.EDXLTEPLib
{
  /// <summary>
  /// Class to represent a TEP Message
  /// </summary>
  public class EDXLTEP : IContentObject, IGeoRSS
  {
    #region Private Member Variables

    /// <summary>
    /// Stores Concatenated Schema Validation Errors
    /// </summary>
    private string schemaErrorString = String.Empty;

    /// <summary>
    /// Stores Number of Schema Validation Errors
    /// </summary>
    private int schemaErrors = 0;

    /// <summary>
    /// Each TEP message contains an identifier that uniquely identifies the 
    /// message.
    /// REQUIRED, MUST be used once and only once.
    /// </summary>
    private string messageID;

    /// <summary>
    /// A unique system id or login credentials of person enetering TEP data, 
    /// used to identify source of information.
    /// OPTIONAL, MAY be used once and only once.
    /// </summary>
    private string systemID;

    /// <summary>
    /// Group of elements used to uniquely describe the patient.
    /// REQUIRED, MUST be used once and only once.
    /// </summary>
    private ClientType client;

    #endregion //Private Memeber Variables

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the EDXLTEP class
    /// </summary>
    public EDXLTEP() { }

    #endregion // Constructors

    #region Public Accessors

    /// <summary>
    /// Gets or sets
    /// Each TEP message contains an identifier that uniquely identifies the 
    /// message.
    /// REQUIRED, MUST be used once and only once.
    /// </summary>
    [XmlElement(ElementName = "messageID", Order = 0)]
    public string MessageID
    {
      get { return this.messageID; }
      set { this.messageID = value; }
    }

    /// <summary>
    /// Gets or sets
    /// A unique system id or login credentials of person enetering TEP data, 
    /// used to identify source of information.
    /// OPTIONAL, MAY be used once and only once.
    /// </summary>
    [XmlElement(ElementName = "systemID", Order = 1)]
    public string SystemID
    {
      get { return this.systemID; }
      set { this.systemID = value; }
    }

    public DateTime ExpiresDateTime
    {
      get { return Client.ClientEncounter.EncounterDateTime.Value.AddHours(24); }
    }

    /// <summary>
    /// Gets or sets
    /// Group of elements used to uniquely describe the patient.
    /// REQUIRED, MUST be used once and only once.
    /// </summary>
    [XmlElement(ElementName = "Client", Order = 2)]
    public ClientType Client
    {
      get { return this.client; }
      set { this.client = value; }
    }

    #endregion // Public Accessors

    #region Public Member Functions

    /// <summary>
    /// Override of IContentObject Interface Function
    /// </summary>
    /// <param name="ckw">ValueList object for content keywords</param>
    /// <returns>String to process ckw value from</returns>
    public string SetContentKeywrods(ValueList ckw)
    {
      ckw.ValueListURN = EDXLConstants.ContentKeywordListName;
      ckw.Value.Add(EDXLConstants.TEPSyndicationCategoryName);
      return EDXLConstants.TEPSyndicationTitle;
    }

    public void WriteXML(XmlWriter xwriter)
    {
      this.Validate();

      xwriter.WriteStartDocument(false);
      xwriter.WriteStartElement("TEPMessage", EDXLConstants.MEXLTEP10Namespace);

      // required element so no null check.
      xwriter.WriteElementString("messageID", this.messageID);

      if (!String.IsNullOrEmpty(this.systemID))
      {
        xwriter.WriteElementString("systemID", this.systemID);
      }

      // required element so no null check.
      this.client.WriteXML(xwriter, "Client");

      xwriter.WriteEndElement();
    }

    public void ReadXML(XmlNode rootnode)
    {
      XmlNode teproot = rootnode;

      foreach (XmlNode node in teproot.ChildNodes)
      {
        if (String.IsNullOrEmpty(node.InnerText))
        {
          continue;
        }

        switch (node.LocalName)
        {
          case "systemID":
            this.systemID = node.InnerText;
            break;
          case "messageID":
            this.messageID = node.InnerText;
            break;
          case "Client":
            this.client = new ClientType();
            this.client.ReadXML(node);
            break;
        }
      }

      this.Validate();
    }


    #endregion // Public Member Functions

    #region Private Member Functions

    public void Validate() {
      if (String.IsNullOrEmpty(this.messageID))
      {
        throw new ArgumentException("Message ID is required and can't be null or empty");
      }

      if (this.client == null)
      {
        throw new ArgumentNullException("Client is required and can't be null");
      }
    }

    #endregion // Private Member Functions

    public void ToGeoRSS(List<System.ServiceModel.Syndication.SyndicationItem> items, Guid linkUID)
    {
      throw new NotImplementedException();
    }

    public string SetContentKeywords(ValueList ckw)
    {
      throw new NotImplementedException();
    }

    public void ValidateToSchema()
    {
      throw new NotImplementedException();
    }
  }
}
